// CyTargetLinker,
// a Cytoscape plugin to extend biological networks with regulatory interaction
// Copyright 2011-2012 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.cytoscape.cytargetlinker.internal.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.application.swing.CySwingApplication;
import org.cytoscape.cytargetlinker.internal.CTLManager;

/**
 * dialog to select which files in the RIN directory
 * should be used
 */
public class RINSelectionDialog extends JDialog {
	
	private CTLManager manager;
	private Map<String, File> riNetworks;
	private List<File> selectedFiles;
	private Map<String, JCheckBox> cbMap;
	
	private CySwingApplication swingApplication;
	
	public RINSelectionDialog(CTLManager manager, Map<String, File> availMirnaNetworks, CyApplicationManager ApplicationManager, CySwingApplication swingApplication) {
		super(swingApplication.getJFrame());
		this.swingApplication = swingApplication;
		this.manager = manager;
		
		selectedFiles = new ArrayList<File>();
		riNetworks = availMirnaNetworks;
		cbMap = new HashMap<String, JCheckBox>();
	
		this.add(getContent());
		locate();
		this.setResizable(false);
		this.pack();
	}

	private JPanel getContent() {
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new BorderLayout(1,1));
		
		panel.add(getMainPanel(), BorderLayout.CENTER);
		panel.add(getCommandRow(), BorderLayout.SOUTH);
		return panel;
	}

	private Component getMainPanel() {
		JPanel mainPanel = new JPanel();
		mainPanel.setBorder(BorderFactory.createTitledBorder("RIN files"));
		BoxLayout layout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
		mainPanel.setLayout(layout);
		
		for(String str : riNetworks.keySet()) {
			JPanel panel = new JPanel();
			BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.X_AXIS);
			panel.setLayout(boxLayout);
			JLabel label = new JLabel(riNetworks.get(str).getName());
			label.setAlignmentX(Component.LEFT_ALIGNMENT);
			JCheckBox box = new JCheckBox();
			cbMap.put(str, box);
			panel.add(box);
			panel.add(label);
			mainPanel.add(panel);
		}
		JScrollPane pane = new JScrollPane(mainPanel);
		return pane;
	}
	
	
	
	private void locate() {
		int sizeX = 400;
		int sizeY = 400;
		this.setPreferredSize(new Dimension(sizeX, sizeY));

		Point cyLocation = swingApplication.getJFrame().getLocation();
		int cyHeight = swingApplication.getJFrame().getHeight();
		int cyWidth = swingApplication.getJFrame().getWidth();

		Point middle = new Point(cyLocation.x + (cyWidth / 2), cyLocation.y + (cyHeight / 2));

		int locX = middle.x - (sizeX / 2);
		int locY = middle.y - (sizeY / 2);
		this.setLocation(new Point(locX, locY));
	}
	
	private JComponent getCommandRow() {
		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				for(String str : cbMap.keySet()) {
					if(cbMap.get(str).isSelected()) {
						selectedFiles.add(riNetworks.get(str));
					}
				}
				dispose();
				manager.startMerge(selectedFiles);
			}
		});
		getRootPane().setDefaultButton(ok);
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				dispose();
			}
		});
		List<JComponent> buttons = new ArrayList<JComponent>();
		buttons.add(ok);
		buttons.add(cancel);
		JPanel panel = new JPanel();
		LayoutManager layout = new BoxLayout(panel, BoxLayout.X_AXIS);
		panel.setLayout(layout);
		panel.setBorder(BorderFactory.createEmptyBorder(17, 0, 0, 0));
		panel.add(Box.createHorizontalGlue());
		Iterator<JComponent> buttonsIter = buttons.iterator();
		while (buttonsIter.hasNext()) {
			panel.add(buttonsIter.next());
			if (buttonsIter.hasNext()) {
				panel.add(Box.createHorizontalStrut(5));
			}
		}
		return panel;
	}
}
